import request from '@/utils/request';

// # 入库
export function getStorageInTypes() {
    return request({
        url: '/v1/goods/storage/in/types',
        method: 'get'
    });
}

// ## 入库新增商品列表
export function getStorageAvaGooodsList({ pageNum, pageSize, categoryId, supplierId, nameOrCode }) {
    return request({
        url: '/v1/goods/storage/list',
        method: 'post',
        data: {
            pageNum,
            pageSize,
            categoryId: !!categoryId ? categoryId : undefined,
            supplierId: !!supplierId ? supplierId : undefined,
            nameOrCode: !!nameOrCode ? nameOrCode : undefined
        }
    });
}

// ## 入库管理
export function getStorageInManageList({
    pageNum,
    pageSize,
    startTime,
    endTime,
    batchNo,
    operType
}) {
    return request({
        url: '/v1/goods/storage/in/list',
        method: 'post',
        data: {
            pageNum,
            pageSize,
            startTime: !!startTime ? startTime : undefined,
            endTime: !!endTime ? endTime : undefined,
            batchNo: !!batchNo ? batchNo : undefined,
            operType: !!operType ? operType : undefined
        }
    });
}

// ## 新增入库
export function addStorageIn({ items, remark, operType }) {
    items = items.map(item => {
        return {
            goodsId: item.goodsId,
            goodsSkuId: item.goodsSkuId,
            intimeStock: !!item.intimeStock ? item.intimeStock : 0,
            operStock: !!item.operStock ? item.operStock : 0,
            supplyPrice: item.supplyPrice
        };
    });
    return request({
        url: '/v1/goods/storage/in/create',
        method: 'post',
        data: {
            items,
            operType,
            remark: !!remark ? remark : undefined
        }
    });
}

// ## 入库查询
export function getStorageInDetail(batchId) {
    return request({
        url: `v1/goods/storage/in/${batchId}/detail`,
        method: 'get'
    });
}

// ## 单据查询
export function getStorageReceipt(batchId) {
    return request({
        url: `/v1/goods/storage/out/${batchId}/receipt`,
        method: 'get'
    });
}

// ## 普通查询
export function getStorageDetail(batchId) {
    return request({
        url: `/v1/goods/storage/out/${batchId}/detail`,
        method: 'get'
    });
}

// ## 入库查询列表
export function getStorageInLooksList({
    pageNum,
    pageSize,
    categoryId,
    startTime,
    endTime,
    batchNo,
    operType
}) {
    return request({
        url: '/v1/goods/storage/in/list/bydetail',
        method: 'post',
        data: {
            pageNum,
            pageSize,
            categoryId: !!categoryId ? categoryId : undefined,
            startTime: !!startTime ? startTime : undefined,
            endTime: !!endTime ? endTime : undefined,
            batchNo: !!batchNo ? batchNo : undefined,
            operType: !!operType ? operType : undefined
        }
    });
}

// # 出库
export function getStorageOutTypes() {
    return request({
        url: '/v1/goods/storage/out/types',
        method: 'get'
    });
}

// ## 新增其他出库
export function addStorageOutOther({ items, remark, operType }) {
    items = items.map(item => {
        return {
            goodsId: item.goodsId,
            goodsSkuId: item.goodsSkuId,
            intimeStock: !!item.intimeStock ? item.intimeStock : 0,
            operStock: !!item.operStock ? item.operStock : 0,
            supplyPrice: item.supplyPrice
        };
    });
    return request({
        url: '/v1/goods/storage/out/other/create',
        method: 'post',
        data: {
            items,
            operType,
            remark: !!remark ? remark : undefined
        }
    });
}

// ## 出库管理列表
export function getStorageOutManageList({
    pageNum,
    pageSize,
    startTime,
    endTime,
    batchNo,
    operType
}) {
    return request({
        url: '/v1/goods/storage/out/list',
        method: 'post',
        data: {
            pageNum,
            pageSize,
            startTime: !!startTime ? startTime : undefined,
            endTime: !!endTime ? endTime : undefined,
            batchNo: !!batchNo ? batchNo : undefined,
            operType: !!operType ? operType : undefined
        }
    });
}

// ## 出库查询列表
export function getStorageOutLooksList({
    pageNum,
    pageSize,
    categoryId,
    startTime,
    endTime,
    batchNo,
    operType
}) {
    return request({
        url: '/v1/goods/storage/out/list/bydetail',
        method: 'post',
        data: {
            pageNum,
            pageSize,
            categoryId: !!categoryId ? categoryId : undefined,
            startTime: !!startTime ? startTime : undefined,
            endTime: !!endTime ? endTime : undefined,
            batchNo: !!batchNo ? batchNo : undefined,
            operType: !!operType ? operType : undefined
        }
    });
}

// # 盘点
// ## 新增库存盘点
export function addStorageCheck({ items, remark }) {
    items = items.map(item => {
        return {
            goodsId: item.goodsId,
            goodsSkuId: item.goodsSkuId,
            intimeStock: !!item.intimeStock ? item.intimeStock : 0,
            realStock: !!item.realStock ? item.realStock : 0,
            supplyPrice: item.supplyPrice
        };
    });
    return request({
        url: '/v1/goods/storage/check/create',
        method: 'post',
        data: {
            items,
            remark: !!remark ? remark : undefined
        }
    });
}

// ## 出库管理列表
export function getStorageCheckManageList({
    pageNum,
    pageSize,
    startTime,
    endTime,
    batchNo,
    operType
}) {
    return request({
        url: '/v1/goods/storage/check/list',
        method: 'post',
        data: {
            pageNum,
            pageSize,
            startTime: !!startTime ? startTime : undefined,
            endTime: !!endTime ? endTime : undefined,
            batchNo: !!batchNo ? batchNo : undefined,
            operType: !!operType ? operType : undefined
        }
    });
}

// ## 出库管理列表
export function getStorageSalesList(planDeliveryTime) {
    return request({
        url: '/v1/goods/storage/out/sale/waitoutlist',
        method: 'post',
        data: {
            planDeliveryTime
        }
    });
}

// ## 发货出库
export function addStorageSale({ items, planDeliveryTime }) {
    items = items.map(item => {
        return {
            goodsId: item.goodsId,
            goodsSkuId: item.goodsSkuId,
            intimeStock: !!item.intimeStock ? item.intimeStock : 0,
            operStock: !!item.operStock ? item.operStock : 0,
            supplyPrice: item.supplyPrice
        };
    });
    return request({
        url: '/v1/goods/storage/out/sale/create',
        method: 'post',
        data: {
            items,
            operType: 'SALEOUT',
            planDeliveryTime
        }
    });
}

// ## check查询
export function getStorageCheckDetail(batchId) {
    return request({
        url: `v1/goods/storage/check/${batchId}/detail`,
        method: 'get'
    });
}
